Release 10.1A: OpenEdge Data Management:
SQL Reference
CREATE TRIGGER
Creates a trigger for the specified table. A trigger is a special type of automatically executed stored procedure that helps ensure referential integrity for a database.
Triggers contain Java source code that can use SQL Java classes to carry out database operations. Triggers are automatically activated when an
INSERT,UPDATE, orDELETEstatement changes the trigger’s target table. The Java source code details what actions the trigger takes when it is activated.Syntax
owner_nameSpecifies the owner of the trigger. If the name is different from the user name of the user executing the statement, then the user must have
DBAprivileges.trignameNames the trigger.
DROP TRIGGERstatements specify the trigger name defined here. SQL also usestrignamein the name of the Java class that it creates from the Java snippet.BEFORE | AFTERDenotes the trigger action time. The trigger action time specifies whether the triggered action, implemented by java_snippet, executes
BEFOREorAFTERthe invokingINSERT,UPDATE, orDELETEstatement.INSERT |DELETE |UPDATE [ OFcolumn_name[ , ...] ]Denotes the trigger event. The trigger event is the statement that activates the trigger.
If
UPDATEis the triggering statement, this clause can include an optional column list. Only updates to any of the specified columns will activate the trigger. IfUPDATEis the triggering statement and does not include the optional column list, then anyUPDATEon the table will activate the trigger.ON table_nameIdentifies the name of the table where the trigger is defined. A triggering statement that specifies table_name causes the trigger to execute. table_name cannot be the name of a view.
REFERENCING OLDROW [ , NEWROW ] | NEWROW [ , OLDROW ]Provides a mechanism for SQL to pass row values as input parameters to the stored procedure implemented by
java_snippet. The code injava_snippetuses the getValue method of theNEWROWandOLDROWobjects to retrieve values of columns in rows affected by the trigger event and store them in procedure variables. This clause is allowed only if the trigger specifies theFOR EACH ROWclause.The meaning of the
OLDROWandNEWROWarguments of theREFERENCINGclause depends on whether the trigger event isINSERT,UPDATE, orDELETE:
INSERT...REFERENCINGNEWROWmeans the triggered action can access values of columns of each row inserted. SQL passes the column values specified by theINSERTstatement.INSERT...REFERENCINGOLDROWis meaningless, since there are no existing values for a row being inserted.INSERT...REFERENCINGOLDROWgenerates a syntax error.UPDATE...REFERENCINGOLDROWmeans the triggered action can access the values of columns, before they are changed, of each row updated. SQL passes the column values of the row as it exists in the database before the update operation.DELETE...REFERENCINGOLDROWmeans the triggered action can access values of columns of each row deleted. SQL passes the column values of the row as it exists in the database before the delete operation.DELETE...REFERENCINGNEWROWis meaningless, since there are no new existing values to pass for a row being deleted.DELETE...REFERENCINGOLDROWgenerates a syntax error.UPDATEis the only triggering statement that allows bothNEWROWandOLDROWin theREFERENCINGclause.UPDATE...REFERENCINGNEWROWmeans the triggered action can access the values of columns, after they are changed, of each row updated. SQL passes the column values specified by theUPDATEstatement.- The trigger action time (
BEFOREorAFTER) does not affect the meaning of theREFERENCINGclause. For instance,BEFOREUPDATE...REFERENCINGNEWROWstill means the values of columns after they are updated will be available to the triggered action.- The
REFERENCINGclause generates an error if the trigger does not include theFOREACHROWclause.FOR EACH { ROW | STATEMENT }Controls the execution frequency of the triggered action implemented by java_snippet.
FOREACHROWmeans the triggered action executes once for each row being updated by the triggering statement.CREATETRIGGERmust include theFOREACHROWclause if it also includes aREFERENCINGclause.
FOREACHSTATEMENTmeans the triggered action executes only once for the whole triggering statement.FOREACHSTATEMENTis the default.IMPORT java_import_clauseSpecifies standard Java classes to import. The
IMPORTkeyword must be uppercase and on a separate line.BEGIN
java_snippet
ENDDenotes the body of the trigger or the triggered action. The body contains the Java source code that implements the actions to be completed when a triggering statement specifies the target table. The Java statements become a method in a class that SQL creates and submits to the Java compiler.
The
BEGINandENDkeywords must be uppercase and on separate lines.Notes
- Triggers can take action on their own table so that they invoke themselves. SQL limits such recursion to five levels.
- You can you have multiple triggers on the same table. Multiple
UPDATEtriggers on the same table must specify different columns. SQL executes all triggers applicable to a given combination of table, trigger event, and action time.- The actions carried out by a trigger can fire another trigger. When this happens, the other trigger’s actions execute before the rest of the first trigger finishes executing.
- If a constraint and trigger are both invoked by a particular SQL statement, SQL checks constraints first, so any data modification that violates a constraint does not also fire a trigger.
- To modify an existing trigger, you must delete it and issue another
CREATE TRIGGERstatement. You can query the systrigger system table for information about the trigger before you delete it.The following example illustrates an
ExampleUPDATEtrigger on a table calledBUG_INFO. If theSTATUSorPRIORITYfields are modified, the trigger modifies theBUG_SUMMARYandBUG_STATUStables appropriately, based on defined conditions:The following code segment illustrates how to use the
CREATE TRIGGERstatement:
The following code segment illustrates how to set values for a new row in the
CREATE TRIGGERstatement:
Authorization
Must have the
DBAprivilege orRESOURCEprivilegeRelated statements
DROP TRIGGER
Note: For more information on creating and using triggers, see OpenEdge Data Management: SQL Development .
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |